<template>
  <el-dialog
    :title="!dataForm.recommendParameterId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="策略划分界限值（小于该值，基于标签；大于该值，用户行为协同过滤）" prop="policyDivision">
      <el-input v-model="dataForm.policyDivision" placeholder="策略划分界限值（小于该值，基于标签；大于该值，用户行为协同过滤）"></el-input>
    </el-form-item>
    <el-form-item label="根据有行为的景点，给推荐列表附加相似度高的景点个数" prop="similarityAttractionNum">
      <el-input v-model="dataForm.similarityAttractionNum" placeholder="根据有行为的景点，给推荐列表附加相似度高的景点个数"></el-input>
    </el-form-item>
    <el-form-item label="用户行为协同过滤中，寻找最邻近用户的个数" prop="neighboringNum">
      <el-input v-model="dataForm.neighboringNum" placeholder="用户行为协同过滤中，寻找最邻近用户的个数"></el-input>
    </el-form-item>
    <el-form-item label="1:当前使用配置，0:不是，保存的配置" prop="nowConfig">
      <el-input v-model="dataForm.nowConfig" placeholder="1:当前使用配置，0:不是，保存的配置"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          recommendParameterId: 0,
          policyDivision: '',
          similarityAttractionNum: '',
          neighboringNum: '',
          nowConfig: ''
        },
        dataRule: {
          policyDivision: [
            { required: true, message: '策略划分界限值（小于该值，基于标签；大于该值，用户行为协同过滤）不能为空', trigger: 'blur' }
          ],
          similarityAttractionNum: [
            { required: true, message: '根据有行为的景点，给推荐列表附加相似度高的景点个数不能为空', trigger: 'blur' }
          ],
          neighboringNum: [
            { required: true, message: '用户行为协同过滤中，寻找最邻近用户的个数不能为空', trigger: 'blur' }
          ],
          nowConfig: [
            { required: true, message: '1:当前使用配置，0:不是，保存的配置不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.recommendParameterId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.recommendParameterId) {
            this.$http({
              url: this.$http.adornUrl(`/recommend/recommendparameter/info/${this.dataForm.recommendParameterId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.policyDivision = data.recommendParameter.policyDivision
                this.dataForm.similarityAttractionNum = data.recommendParameter.similarityAttractionNum
                this.dataForm.neighboringNum = data.recommendParameter.neighboringNum
                this.dataForm.nowConfig = data.recommendParameter.nowConfig
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/recommend/recommendparameter/${!this.dataForm.recommendParameterId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'recommendParameterId': this.dataForm.recommendParameterId || undefined,
                'policyDivision': this.dataForm.policyDivision,
                'similarityAttractionNum': this.dataForm.similarityAttractionNum,
                'neighboringNum': this.dataForm.neighboringNum,
                'nowConfig': this.dataForm.nowConfig
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
